import DatabaseRepository from "../../../db/DatabaseRepository"
import { app } from "electron"
import path from "node:path"


// Get the application data path
const userDataPath = app.getPath("userData");
console.log("userDataPath----------------------------------->", userDataPath)
const dbPath = path.join(userDataPath, 'download-tool.db');

export async function initializeDatabase() {
  const database = new DatabaseRepository(dbPath);

  try {
    // Connect to the database
    await database.connect();

    // Create the tasks table
    await database.run(`
      CREATE TABLE IF NOT EXISTS tasks (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        gid TEXT,
        name TEXT,
        status TEXT
      )
    `);

    // Example: Insert a task
    const insertedTask = {
      gid: '1',
      name: 'Sample Task',
      status: 'active',
    };
    const result = await database.run('INSERT INTO tasks (gid, name, status) VALUES (?, ?, ?)', [
      insertedTask.gid,
      insertedTask.name,
      insertedTask.status,
    ]);
    console.log('Inserted task ID:', result.lastID);

    // Example: Query all tasks
    const allTasks = await database.all('SELECT * FROM tasks');
    console.log('All tasks:', allTasks);
  } catch (error) {
    console.error('Database error:', error);
  } finally {
    // Disconnect from the database
    await database.disconnect();
  }
}

